<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
   class Sys_set extends Admin_Controller
   {
      function __construct()
      {
         parent::__construct();
         $this->load->model('User_model');
         $this->load->model('Admin_role_model');
         $this->load->model('sysconf_model');
	     $this->roleid= 0;
      }
      function index()
      {
         $this->load->view('admin/admin_login');
      }
      function purview_manage()
      {
      	  $res['data']=$this->Admin_role_model->post(NULL,TRUE);
      	  foreach($res['data'] as $k=>$val)
      	  {
      	  	if($val['static']=='0')
      	 	 $res['data'][$k]['toggle_static']="开启";
      	  	else
      	  	 $res['data'][$k]['toggle_static']="禁用";
      	  }
	      $top_meun = $this->cache->file->get('top_meun_'.$this->roleid);
      	  $this->load->view('admin/purview_manage',$res);
      }
      function edit_menumes(){
      	$post=$this->input->post(null,true);
      	$id=$this->input->post('id',TRUE);
      	$title=$this->input->post('title',TRUE);
      	$c=$this->input->post('c',TRUE);
      	$a=$this->input->post('a',TRUE);
      	if ($post['save'] == 'save'){
      		$sql = "update qp_admin_role set title = '$title' , c = '$c' , a = '$a' where id = $id ";
      	  	$res=$this->db->query($sql);
      		if ($res){
                	$link[0]['href'] = site_aurl('sys_set/purview_manage');
                	$this->common->adminmsg('操作成功','2',$link);
      	  	  }else {
                	$link[0]['href'] = site_aurl('sys_set/purview_manage');
                	$this->common->adminmsg('操作失败','1',$link);
      	  	  }
      	}else{
      		$res['data']=$this->User_model->edit_menumes($_GET['id']);
      		$this->load->view('admin/edit_menu',$res);
      	}
      }
      function sys_config()
      {
      		$post=$this->input->post(null,true);
      	    if($post['syssave'] == 'syssave'){
      	  	  foreach ($post['config'] as $key => $val){
      	  		  $sql = "update qp_config set coValue = '".$val."' where cgId = 1 and coKey = '".$key."'";
      	  		  $res=$this->db->query($sql);
      	  	  }
      	  	  if ($res){
                	$link[0]['href'] = site_aurl('sys_set/sys_config');
                	$this->common->adminmsg('操作成功','2',$link);
      	  	  }else {
                	$link[0]['href'] = site_aurl('sys_set/sys_config');
                	$this->common->adminmsg('操作失败','1',$link);
      	  	  }
      	    }else{
      	  		$res['data']=$this->sysconf_model->sysvalueweb();
      	  		$res['tpl']='config_web';
      	  		$this->load->view('admin/sys_config',$res);
      	    }
      }
      function sysconf_mail()
      {
      	  $post=$this->input->post(null,true);
      	    if($post['mailsave'] == 'mailsave'){
      	  	  foreach ($post['mail'] as $key => $val){
      	  		  $sql = "update qp_config set coValue = '".$val."' where cgId = 2 and coKey = '".$key."'";
      	  		  $res=$this->db->query($sql);
      	  	  }
      	  	  if ($res){
                	$link[0]['href'] = site_aurl('sys_set/sysconf_mail');
                	$this->common->adminmsg('操作成功','2',$link);
      	  	  }else {
                	$link[0]['href'] = site_aurl('sys_set/sysconf_mail');
                	$this->common->adminmsg('操作失败','1',$link);
      	  	  }
      	  	  
      	     }else{
      	    	$res['data']=$this->sysconf_model->sysvaluemail();
      	    	$res['tpl']='config_mail';
      	    	$this->load->view('admin/sys_config',$res);
      	    }
      }
      /*test*/
      function send_mail()
      {
      		$post=$this->input->post(null,true);
      	    if($post['sendmail'] == 'sendmail'){
      	  	  	$config['protocol'] = 'smtp';//采用smtp方式
	      		$config['smtp_host'] = 'smtp.163.com';//简便起见，只支持163邮箱
	      		$config['smtp_user'] = 'studycms@163.com';//你的邮箱帐号
	      		$config['smtp_pass'] = 'studycms.';//你的邮箱密码
	      		$config['charset'] = 'utf-8';
	      		$config['wordwrap'] = TRUE;
	      		$config['mailtype'] = "html";
	      		$this->load->library('email');//加载email类
	      		$this->email->initialize($config);//参数配置
	      		$this->email->from('studycms@163.com', 'studycms管理员');
	      		$this->email->to('644506142@qq.com');
	      		$this->email->subject('测试邮件');
	      		$this->email->message('新发送邮件信息');    //显示发送邮件的结果，加载到res_view.php视图文件中
	      		if ($this->email->send()){
      	  	  		echo "<script>alert('send mail yes');window.history.go(-1)</script>";
      	  	  	}else {
      	  	  		echo "<script>alert('send mail no');window.history.go(-1)</script>";
      	  	  	}
      	    }else{
      	    	$res['data']=$this->sysconf_model->sysvaluemail();
      	  		$res['tpl']='send_mail';
      	  		$this->load->view('admin/sys_config',$res);
      	    }
      }
          function add_admin()
	      {
	      	$post=$this->input->post(NULL,TRUE);
	      	if($post['method'])
	      	$url=$post['control'].'/'.$post['method'];
	      	else
	      	$url='';
	      	$res=$this->User_model->add($post['selected'],$post['title'],$post['control'],$post['method'],$url);
	      	if($res)
	      	{
	      		echo 1; //添加成功
	      	}else
	      	{
                echo 2; //添加失败	      		
	      	}
	      }
	      function toggle_url()
	      {
              $id=$this->input->post('id',TRUE);      	
	      	  $res=$this->Admin_role_model->get_by('id',$id);
	      	  if(!empty($res))
	      	  { 
	      	  	 $static = $res['static'] ? '0':'1';
	      	  	 $data=array('static'=>$static);
	      	  	 $data=$this->Admin_role_model->update($id,$data);
	      	  	 if($data)
	      	  	 echo 1;//更改开启状态
	      	  	 else
	      	  	 echo 0;
	      	  }
	      }
   		  function delete_url()
	      {
              $id=$this->input->post('id',TRUE);
	      	  $res=$this->Admin_role_model->get_by('id',$id);
	      	  if(!empty($res))
	      	  {
	      	  	 if($this->Admin_role_model->del_meun($res['path']))
	      	  	 echo 'true';//删除
	      	  	 else
	      	  	 echo "false";//删除失败
	      	  }else
	      	  {
	      	  	 echo '2';   //此菜单栏不存在
	      	  }
	      }
	      function purview_up()
	      {
	      	$id=$this->input->post('id',TRUE);
	      	$up_data=$this->Admin_role_model->getfield_by('id',$id);
	      	if(!$up_data)
	      	{
	      		 echo '1'; exit;//此栏目不存在.
	      	}
	      	$arr_pid=array();
	      	//组装二维数组
	      	$arr_arr=array();  
	      	$all_date=$this->Admin_role_model->post(NULL,TRUE);
	      	foreach($all_date as $k=>$val)
	      	{
	      		if($up_data['pid']==$val['pid'])
	      		{
	      	         $arr_arr[]=$val;
	      		}
	      	}
	   
	      //目标数组key
	      	$arr_key='';
	      	$new_arr=array();
	      	$target_id='';
	      	$target_arr=array();
      		foreach($arr_arr as $k=>$val)
      		{
      			if($up_data['id']==$val['id'])
      			{
      				$arr_key=$k-1;
      				if($arr_key<0)
      				{
      					echo "2"; exit; //修改失败,已经在最顶端
      				}
      		        $new_arr=$val;
      			}
      		}
	        foreach($arr_arr as $k=>$val)
      		{
      	    	if($arr_key==$k)
      		 	{
      		 		$target_id=$val['id'];
      				$target_arr=$val;
      		 	}
      		}
	        $new_path=$this->Admin_role_model->get_path($new_arr['path']);
	        $target_path=$this->Admin_role_model->get_path($target_arr['path']);
	         //源path数组
	         $s_path=explode(',',$new_arr['path']);
	         //目标path数组
             $t_path=explode(',',$target_arr['path']);
             //源path的最后一位
             foreach($new_path as $val)
             {
             	$s_art_arr=array();
             	if($val['path']==$new_arr['path'])
             	{
             		$left_new_arr=array_splice($val, 4);
             		$this->Admin_role_model->get_update($target_arr['id'],$left_new_arr);
             		$new_affect=$this->db->affected_rows();
             	}else
             	{
             		$s_art_path=explode(',',$val['path']);
        			$s_path_num=count($t_path);
					$s_left_path=array_splice($s_art_path,$s_path_num);
					$new_s_str_path= $target_arr['path'].','.implode(',',$s_left_path);
					$s_depth=$new_arr['depth']+1;
             		if($s_depth==$val['depth'])
             		{
             			$s_art_arr['pid']=$target_id;
             		}
             		$s_art_arr['path']=$new_s_str_path;
             		$this->Admin_role_model->get_update($val['id'],$s_art_arr);
             	}          
             }
	         foreach($target_path as $val)
             {
             	$t_art_arr=array();
             	if($val['path']==$target_arr['path'])
             	{
             		$left_target_arr=array_splice($val, 4);
             		$this->Admin_role_model->get_update($new_arr['id'],$left_target_arr);
             		$target_affect=$this->db->affected_rows();
             	}else
             	{
             		$t_art_path=explode(',',$val['path']);
        			$t_path_num=count($s_path);
					$t_left_path=array_splice($t_art_path,$t_path_num);
					$new_t_str_path= $new_arr['path'].','.implode(',',$t_left_path);
					$t_depth=$target_arr['depth']+1;
             		if($t_depth==$val['depth'])
             		{
             		   $t_art_arr['pid']=$up_data['id'];	
             		}
             		$t_art_arr['path']=$new_t_str_path;
             		$this->Admin_role_model->get_update($val['id'],$t_art_arr);
             	}            	
             }
             if($target_affect && $new_affect)
             {
             	echo "3";exit;
             }
	      }
	      function purview_down()
	      {
		        $id=$this->input->post('id',TRUE);
		      	$up_data=$this->Admin_role_model->getfield_by('id',$id);
		      	if(!$up_data)
		      	{
		      		 echo '1'; exit;//此栏目不存在.
		      	}
		      	$arr_pid=array();
		      	//组装二维数组
		      	$arr_arr=array();  
		      	$all_date=$this->Admin_role_model->post(NULL,TRUE);
		      	foreach($all_date as $k=>$val)
		      	{
		      		if($up_data['pid']==$val['pid'])
		      		{
		      	         $arr_arr[]=$val;
		      		}
		      	}
		   
		      //目标数组key
		      	$arr_key='';
		      	$new_arr=array();
		      	$target_id='';
		      	$target_arr=array();
	      		foreach($arr_arr as $k=>$val)
	      		{
	      			if($up_data['id']==$val['id'])
	      			{
	      				$arr_key=$k+1;
	      				if($arr_key>count($arr_arr)-1)
	      				{
	      					echo "2"; exit; //修改失败,已经在最底端
	      				}
	      		        $new_arr=$val;
	      			}
	      		}
		        foreach($arr_arr as $k=>$val)
	      		{
	      	    	if($arr_key==$k)
	      		 	{
	      		 		$target_id=$val['id'];
	      				$target_arr=$val;
	      		 	}
	      		}
		        $new_path=$this->Admin_role_model->get_path($new_arr['path']);
		        $target_path=$this->Admin_role_model->get_path($target_arr['path']);
		         //源path数组
		         $s_path=explode(',',$new_arr['path']);
		         //目标path数组
	             $t_path=explode(',',$target_arr['path']);
	             //源path的最后一位
	             foreach($new_path as $val)
	             {
	             	$s_art_arr=array();
	             	if($val['path']==$new_arr['path'])
	             	{
	             		$left_new_arr=array_splice($val, 4);
	             		$this->Admin_role_model->get_update($target_arr['id'],$left_new_arr);
	             		$new_affect=$this->db->affected_rows();
	             	}else
	             	{
	             		$s_art_path=explode(',',$val['path']);
	        			$s_path_num=count($t_path);
						$s_left_path=array_splice($s_art_path,$s_path_num);
						$new_s_str_path= $target_arr['path'].','.implode(',',$s_left_path);
						$s_depth=$new_arr['depth']+1;
	             		if($s_depth==$val['depth'])
	             		{
	             			$s_art_arr['pid']=$target_id;
	             		}
	             		$s_art_arr['path']=$new_s_str_path;
	             		$this->Admin_role_model->get_update($val['id'],$s_art_arr);
	             	}          
	             }
		         foreach($target_path as $val)
	             {
	             	$t_art_arr=array();
	             	if($val['path']==$target_arr['path'])
	             	{
	             		$left_target_arr=array_splice($val, 4);
	             		$this->Admin_role_model->get_update($new_arr['id'],$left_target_arr);
	             		$target_affect=$this->db->affected_rows();
	             	}else
	             	{
	             		$t_art_path=explode(',',$val['path']);
	        			$t_path_num=count($s_path);
						$t_left_path=array_splice($t_art_path,$t_path_num);
						$new_t_str_path= $new_arr['path'].','.implode(',',$t_left_path);
						$t_depth=$target_arr['depth']+1;
	             		if($t_depth==$val['depth'])
	             		{
	             		   $t_art_arr['pid']=$up_data['id'];	
	             		}
	             		$t_art_arr['path']=$new_t_str_path;
	             		$this->Admin_role_model->get_update($val['id'],$t_art_arr);
	             	}            	
	             }
	             if($target_affect && $new_affect)
	             {
	             	echo "3";exit;
	             }
	      }
   }
?>